<?php

namespace app\common\command;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\DividendLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Pingjundividend extends Command
{
    protected function configure()
    {
        $this->setName('pingjundividend')->setHelp('分红')->setDescription('分红');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel        = new Order();
        $userModel         = new \app\common\model\User();
        $dividendLogModel  = new DividendLog();
        $accelerationModel = new Acceleration();
        $info              = $dividendLogModel->where(['dividend_type'=>1])->whereTime('createtime', 'd')->find();
        if(config('site.tiaoshi') == 1){
            $info = '';
        }
        if (!$info) {
            $order_amount = $orderModel->whereNull('activity_type')->whereIn('status', 'paid,completed')->whereTime('createtime', 'd')->sum('order_amount');
//                        $order_amount = $orderModel->whereIn('status', 'paid,completed')->whereTime('createtime',
//                'yesterday')->sum('order_amount');
            if ($order_amount) {
                $accelerationData = $accelerationModel->whereIn('id', '5,6,7')->where('status', 1)->select();
                if ($accelerationData) {
                    Db::startTrans();
                    try {
                        foreach ($accelerationData as $k => $v) {
                            $level    = $v['id'];
                            $userData = $userModel->where(['level' => $level,'score'=>['gt',0]])->field('id,score')->select();
                            $dividend = $v['dividend'];
                            if (count($userData) > 0 && $dividend) {
                                $result = false;
                                $fencheng    = bcmul($order_amount, $dividend / 100, 2);
                                $golden      = bcmul($fencheng, 0.3, 2);
                                $user_gold = bcdiv($golden, count($userData), 2);
                                $user_ids    = '';
                                foreach ($userData as $kk => $vv) {
                                    if($vv['score'] <= $user_gold){
                                        $user_gold = $vv['score'];
                                    }
                                    $result   = WalletService::change($vv['id'], 'gold', $user_gold, 'junfen_gold',
                                        [],
                                        '分红');
                                    $result   = WalletService::change($vv, 'score', -$user_gold,
                                        'junfen_score',
                                        [],
                                        '分红释放');
                                    $user_ids .= ','.$vv['id'];
                                }
                                if ($result ) {
                                    $dividendLogModel->create(['user_ids'   => trim($user_ids, ','),
                                                               'user_num'   => count($userData),
                                                               'all_golden' => $user_gold*count($userData),
                                                               'level'      => $level
                                    ]);
                                    echo '分佣成功';
                                }
                                Db::commit();
                            }
                        }
                    } catch (HttpResponseException $e) {
                        Db::rollback();
                        $data    = $e->getResponse()->getData();
                        $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                        format_log_error($e, 'Release.release.HttpResponseException', $message);
                    }
                }

            }
        } else {
            echo '今日已分佣';
        }

    }

}
